//
// Created by tonystark on 16/3/31.
// Copyright (c) 2016 sewm. All rights reserved.
//

#import "UIView+Tips.h"
#import "JGProgressHUD.h"

@implementation UIView (Tips)

- (JGProgressHUD *)showLoading {
    return [self showLoadingView:nil detailText:nil];
}

- (JGProgressHUD *)showLoadingView:(NSString *)title detailText:(NSString *)detail {
    JGProgressHUD *jgProgressHUD = [self viewWithTag:kLoadingViewTag];
    //每次都生成
    if (jgProgressHUD)[jgProgressHUD dismissAnimated:NO];
    jgProgressHUD = [[JGProgressHUD alloc] initWithStyle:(JGProgressHUDStyleDark)];
    jgProgressHUD.textLabel.text = nil;
    jgProgressHUD.detailTextLabel.text = detail;
    JGProgressHUDIndeterminateIndicatorView *indicator = [JGProgressHUDIndeterminateIndicatorView new];
    [indicator setColor:[UIColor whiteColor]];
    jgProgressHUD.indicatorView = indicator;
    jgProgressHUD.tag = kLoadingViewTag;
    [jgProgressHUD showInView:self animated:YES];
    return jgProgressHUD;
}

- (void)dismissLoadingView {
    [self dismissLoadingView:0];
}

- (void)dismissLoadingView:(NSTimeInterval)delay {
    JUSt_RUN_ON_UI_THREAD(^{
        JGProgressHUD *jgProgressHUD = [self viewWithTag:kLoadingViewTag];
        if (jgProgressHUD) {
            if (delay == 0) {
                [jgProgressHUD dismissAnimated:NO];
            } else {
                [jgProgressHUD dismissAfterDelay:delay];
            };
        }
    });
}

- (JGProgressHUD *)showSuccessText:(NSString *)title detailText:(NSString *)detail {
    return [self showResult:title detailText:detail result:YES];
}

- (JGProgressHUD *)showFaildText:(NSString *)title detailText:(NSString *)detail {
    return [self showResult:title detailText:detail result:NO];
}

- (JGProgressHUD *)showResult:(NSString *)title detailText:(NSString *)detail result:(BOOL)success {
    JGProgressHUD *jgProgressHUD = [self viewWithTag:kLoadingViewTag];
    if (jgProgressHUD)[jgProgressHUD dismissAnimated:NO];
    jgProgressHUD = [[JGProgressHUD alloc] initWithStyle:(JGProgressHUDStyleDark)];
    jgProgressHUD.textLabel.text = nil;
    jgProgressHUD.detailTextLabel.text = detail;
    jgProgressHUD.indicatorView = success ? [JGProgressHUDSuccessIndicatorView new] : [JGProgressHUDErrorIndicatorView new];
    jgProgressHUD.tag = kLoadingViewTag;
    JUSt_RUN_ON_UI_THREAD(^{
        [jgProgressHUD showInView:self animated:NO];
        [jgProgressHUD dismissAfterDelay:2];
    });
    jgProgressHUD.tapOutsideBlock = ^(JGProgressHUD *HUD) {
        [HUD dismissAnimated:NO];
    };
    return jgProgressHUD;
}

@end